// ignore_for_file: deprecated_member_use

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_layout/generated/l10n.dart';
import 'package:flutter_layout/model/article.dart';
import 'package:flutter_layout/provider/view_state_refresh_list_model.dart';
import 'package:flutter_layout/service/wan_android_repository.dart';
import 'package:flutter_layout/tools/print_utils.dart';
import 'package:flutter_layout/ui/widget/app_update.dart';

import 'home_page.dart';
import 'project_page.dart';
import 'structure_page.dart';
import 'user_page.dart';
import 'wechat_account_page.dart';

List<Widget> pages = <Widget>[
  HomePage(),
  FullPage(),
  FullPage(),
  FullPage(),
  FullPage()
  // ProjectPage(),
  // WechatAccountPage(),
  // StructurePage(),
  // UserPage()
];

class TabNavigator extends StatefulWidget {
  const TabNavigator({Key? key}) : super(key: key);

  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final _pageController = PageController();
  int _selectedIndex = 0;
  DateTime? _lastPressed;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async {
          if (_lastPressed == null || DateTime.now().difference(_lastPressed!) > const Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            _lastPressed = DateTime.now();
            return false;
          }
          return true;
        },
        child: PageView.builder(
          itemBuilder: (ctx, index) => pages[index],
          itemCount: pages.length,
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              _selectedIndex = index;
            });
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: const Icon(Icons.home),
            title: Text(S.of(context).tabHome),
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.format_list_bulleted),
            title: Text(S.of(context).tabProject),
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.group_work),
            title: Text(S.of(context).wechatAccount),
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.call_split),
            title: Text(S.of(context).tabStructure),
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.insert_emoticon),
            title: Text(S.of(context).tabUser),
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);
        },
      ),
    );
  }

  @override
  void initState() {
    // checkAppUpdate(context);//TODO HLY检查更新
    super.initState();
  }
}

class FullPage extends StatefulWidget {
  const FullPage({Key? key}) : super(key: key);

  @override
  State<FullPage> createState() => _FullPageState();
}

class _FullPageState extends State<FullPage> with AutomaticKeepAliveClientMixin {
  List<Banner>? _banners;
  List<Article>? _topArticles;

  List<Banner>? get banners => _banners;

  List<Article>? get topArticles => _topArticles;

  @override
  void initState() {
    super.initState();
    // try {
      PrintUtils.printByApp("执行initState");
      var result = loadData(pageNum: 0);
      PrintUtils.printByApp("执行initState ${result}");
    // } catch (e) {
    //   if (e is FlutterError) {
    //     PrintUtils.printByApp((e as FlutterError).message);
    //   }
    // }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        padding: EdgeInsets.only(top: 20),
        child: Text("哈哈哈哈哈"),
      ),
    );
  }

  Future<List> loadData({int? pageNum}) async {
    List<Future> futures = [];
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      futures.add(WanAndroidRepository.fetchBanners());
      futures.add(WanAndroidRepository.fetchTopArticles());
    }
    // if (null != pageNum && futures.isEmpty) {
    futures.add(WanAndroidRepository.fetchArticles(pageNum!));
    // }

    // try {
    // Future.wait(futures)
    var result = await Future.wait(futures);
    PrintUtils.printByApp("执行loadData  $pageNum    ${result.length}");
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      _banners = result[0];
      _topArticles = result[1];
      return result[2];
    } else {
      PrintUtils.printByApp("执行loadData result");
      return result[0];
    }
    // } catch (e) {
    //   PrintUtils.printByApp("执行loadData  ${e}");
    //   return futures;
    // }
  }

  @override
  bool get wantKeepAlive => true;
}
